// pages/promotion-poster/index.js
// 推广海报
import api from '../../../api/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [], //海报分类
    mNavList: [], //素材分类
    pageNo: 1,
    pid: '',
    mid: '', //素材分类导航id
    list: [],
    isLoading: false,
    isComplete: false,
    type: 0, //  推广海报0 朋友圈素材 1
    userInfo: {},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNav();
    this.getAllPosters();
    this.getMaterialNav();
  },
  //查看图片朋友圈文案
  reviewImages(e) {
    let img = e.currentTarget.dataset.img;
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img], // 需要预览的图片http链接列表
      success: function(res){
        console.log(res,'ssss')
      },
      fail: function(err){
        console.log(err,'cccc')
      } 
    })
  },
  //获取素材
  getCopywritingData() {
    this.setData({
      isLoading: true
    })
    api.getCopywritingData({
      pid: this.data.mid,
      pageNo: this.data.pageNo
    }).then(res => {
      let data = res.data.data.data;
      this.setData({
        list: this.data.list.concat(data.records),
        userInfo: res.data.data,
        pageNo: ++this.data.pageNo,
        isLoading: false,
        isComplete: !data.records.length
      })
    })
  },
  //切换朋友圈素材分类
  onChangeMNav(e) {
    let id = e.currentTarget.dataset.id;
    if (this.mid != id)
      this.setData({
        mid: id,
        pageNo: 1,
        list: [],
        isLoading: false,
        isComplete: false,
      })
    this.getCopywritingData();

  },
  //获取朋友圈素材分类
  getMaterialNav() {
    api.plainDictionary('copywriting_classification').then(res => {
      this.setData({
        mNavList: res.data.data,
        mid: res.data.data[0].itemValue
      })
    })
  },
  //复制文案
  copy(e) {
    let text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: text,
      success(res) {

      }
    })
  },
  //切换菜单
  onChangeMenu(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      type,
      list: [],
      pageNo: 1,
      isLoading: false,
      isComplete: false
    })
    if (type == 0) {
      //获取海报
      this.getAllPosters();
    } else {
      //获取朋友圈素材
      this.getCopywritingData();
    }
  },
  //预览图片推广海报
  preview(e) {
    let url = e.currentTarget.dataset.url;
    let urls = [];
    this.data.list.forEach(item => {
      urls.push(item.base64Code ? item.base64Code : item.url)
    });
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: urls,
      showmenu: true,
      success: function(res){
        console.log(res,'ssss111')
      },
      fail: function(err){
        console.log(err,'cccc111')
      } 
    })
  },
  // 切换导航
  onChangeNav(e) {
    let id = e.currentTarget.dataset.id;
    if (this.pid != id)
      this.setData({
        pid: id,
        pageNo: 1,
        list: [],
        isLoading: false,
        isComplete: false,
      })
    this.getAllPosters();


  },
  //获取海报
  getAllPosters() {
    this.setData({
      isLoading: true
    })
    api.getAllPosters({
      pid: this.data.pid,
      pageNo: this.data.pageNo
    }).then(res => {
      console.log(res);
      // res.data.data.records.map(item=>{
      //   item.base64Code=item.base64Code.replace(/[\r\n]/g, '')
      // })
      this.setData({
        list: this.data.list.concat(res.data.data.records),
        pageNo: ++this.data.pageNo,
        isLoading: false,
        isComplete: !res.data.data.records.length
      })
    })
  },
  //获取分类
  getNav() {
    api.getProNav().then(res => {
      this.setData({
        navList: res.data.data
      })
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.type == 0) {
      !this.data.isLoading && !this.data.isComplete && this.getAllPosters();
    } else {
      !this.data.isLoading && !this.data.isComplete && this.getCopywritingData();
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})